<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="824.41">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; min-height: 22.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #000000; min-height: 14.0px}
p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000; min-height: 12.0px}
span.s1 {color: #000000}
span.s2 {color: #bf0000}
span.s3 {color: #0000bf}
span.s4 {color: #007300}
span.Apple-tab-span {white-space:pre}
</style>
</head>
<body>
<p class="p1">//SLUGens released under the GNU GPL as extensions for SuperCollider 3, by Nick Collins, http://www.informatics.sussex.ac.uk/users/nc81/</p>
<p class="p2"><b></b><br></p>
<p class="p3"><b>NL<span class="Apple-tab-span">	</span><span class="Apple-tab-span">	</span><span class="Apple-tab-span">	</span> Non Linear Filter Equation</b></p>
<p class="p2"><b></b><br></p>
<p class="p4"><b>NL.ar(input, bufnuma, bufnumb, guard1, guard2, mul, add)</b></p>
<p class="p5"><br></p>
<p class="p4">Represents the arbitrary non-linear filter difference equation in the time domain:</p>
<p class="p5"><br></p>
<p class="p4">y(n) = b00x(n)^b01 + b10x(n-1)^b11 + ... + b(Nb0)x(n-Nb0)^Nb1 + a00y(n-1)^a01 + ... + a(Na0)y(n-Na0)^Na1</p>
<p class="p5"><br></p>
<p class="p4">Though no cross-terms combining powers of x and y are allowed.</p>
<p class="p5"><br></p>
<p class="p4">Stability is definitely not guaranteed; most equations will quickly blow-up. See the guard arguments below. It is recommended that you stick to positive exponents for signals which are within -1 to 1, else explosion of values is inevitable.<span class="Apple-converted-space"> </span></p>
<p class="p6"><br></p>
<p class="p1"><span class="s1">(0.1)**(-1.26)<span class="Apple-converted-space">  </span></span>//negative exponents cause blowup for smaller signals abs(sig) &lt; 1.0</p>
<p class="p7"><br></p>
<p class="p1"><span class="s1">(1.1)**(2.26)<span class="Apple-converted-space">  </span></span>//positive exponents cause blowup for larger signals abs(sig) &gt; 1.0</p>
<p class="p5"><br></p>
<p class="p4">You need to pass in the parameters via two buffers, of arbitrary size.</p>
<p class="p5"><br></p>
<p class="p4"><b>input</b>- What do you want to filter?</p>
<p class="p4"><b>bufnuma</b>- Feedback filter coefficients, from previous outputs, in triples of (index,coefficient, exponent) from lowest to highest index</p>
<p class="p4"><b>bufnumb</b>- Feedforward filter coefficients, from previous inputs, in pairs of (index,coefficient, exponent) from lowest to highest index</p>
<p class="p4"><b>guard1</b>-<span class="Apple-converted-space">  </span>Watch out for blow-up and reset if necessary; this is the value of the maximum absolute output allowed.<span class="Apple-converted-space"> </span></p>
<p class="p4"><b>guard2</b>-<span class="Apple-converted-space">  </span>Watch out for blow-up and reset if necessary; this is the value of the maximum absolute change of output allowed.<span class="Apple-converted-space"> </span></p>
<p class="p5"><br></p>
<p class="p4"><span class="Apple-converted-space"> </span>On discovering blow-up, filter output is set back to zero for the last Na stored outputs, so that feedback cannot occur. <span class="Apple-converted-space"> </span></p>
<p class="p5"><br></p>
<p class="p4">This UGen can be expensive to run because of the power operations that have to be carried out to calculate each new sample. You can only change the filter equations on the fly where you change multiplier coefficients and exponents; indices are set at initialisation however.<span class="Apple-converted-space"> </span></p>
<p class="p6"><br></p>
<p class="p8">(</p>
<p class="p8">a=[0,0.02,1,3,-0.01,0.5]; <span class="s2">//feedback coefficients</span></p>
<p class="p8">b=[0,0.3,1, 1,-0.3,2, 2,0.1,0.4]; <span class="s2">//feedforward coefficients</span></p>
<p class="p8">c=<span class="s3">Buffer</span>.sendCollection(s, a, 1);</p>
<p class="p8">d=<span class="s3">Buffer</span>.sendCollection(s, b, 1);</p>
<p class="p8">)</p>
<p class="p9"><br></p>
<p class="p8">{<span class="s3">SinOsc</span>.ar(<span class="s3">MouseX</span>.kr(440,1760),0,0.2)}.play <span class="s2">//without</span></p>
<p class="p9"><br></p>
<p class="p8">{<span class="s3">NL</span>.ar(<span class="s3">SinOsc</span>.ar(<span class="s3">MouseX</span>.kr(440,1760),0,0.5),c.bufnum, d.bufnum).clip2(1.0)}.play <span class="s2">//with</span></p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p8">(</p>
<p class="p8">a=[0,0.02,1, 1,-0.02,0.3]; <span class="s2">//feedback coefficients</span></p>
<p class="p8">b=[0,0.5,1, 3,-0.2,4, 7,0.2,0.26]; <span class="s2">//feedforward coefficients</span></p>
<p class="p8">c=<span class="s3">Buffer</span>.sendCollection(s, a, 1);</p>
<p class="p8">d=<span class="s3">Buffer</span>.sendCollection(s, b, 1);</p>
<p class="p8">)</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p1">//you can hear the nonlinearities (extra tones in the signal)</p>
<p class="p8">{<span class="s3">RLPF</span>.ar(<span class="s3">NL</span>.ar(<span class="s3">Saw</span>.ar(<span class="s3">MouseX</span>.kr(440,1760),0.5),c.bufnum, d.bufnum, 1.0),1000,0.1)}.play</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p1">//random buffers; this code may lead to repeated indices, but shouldn't affect the result (will just add to sum)</p>
<p class="p8">(</p>
<p class="p8">a=<span class="s3">Array</span>.fill(10,{[rrand(0,50),0.2.rand2, rrand(0.1,2.1)]}).sort({<span class="s3">|a,b|</span> a[0] &lt; b[0]}).flatten; <span class="s2">//feedback coefficients</span></p>
<p class="p8">b=<span class="s3">Array</span>.fill(10,{[rrand(1,50),0.2.rand2, rrand(0.1,2.1)]}).sort({<span class="s3">|a,b|</span> a[0] &lt; b[0]}).flatten; <span class="s2">//feedforward coefficients</span></p>
<p class="p8">b=[0,1,1]++b;</p>
<p class="p8">c=<span class="s3">Buffer</span>.sendCollection(s, a, 1);</p>
<p class="p8">d=<span class="s3">Buffer</span>.sendCollection(s, b, 1);</p>
<p class="p8">)</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p8">{<span class="s3">LPF</span>.ar(<span class="s3">NL</span>.ar(<span class="s3">Saw</span>.ar(<span class="s3">MouseX</span>.kr(440,1760),<span class="s3">MouseY</span>.kr(0.0,1.0)),c.bufnum, d.bufnum), 1000)}.play</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p1">//check impulse response</p>
<p class="p8">{<span class="s3">NL</span>.ar(<span class="s3">Impulse</span>.ar(1),c.bufnum, d.bufnum)}.plot(1024/(s.sampleRate))</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p1">//Take advantage of sparsity of array to have larger spaced coefficients; causes rhythmic blowups sometimes!<span class="Apple-converted-space"> </span></p>
<p class="p1">//random buffers; this code may lead to repeated indices, but shouldn't affect the result (will just add to sum)</p>
<p class="p8">(</p>
<p class="p8">a=<span class="s3">Array</span>.fill(10,{[rrand(0,5000),0.7.rand2, exprand(0.1,6.1)]}).sort({<span class="s3">|a,b|</span> a[0] &lt; b[0]}).flatten; <span class="s2">//feedback coefficients</span></p>
<p class="p8">b=<span class="s3">Array</span>.fill(5,{[rrand(1,500),0.2.rand2, exprand(0.1,6.1)]}).sort({<span class="s3">|a,b|</span> a[0] &lt; b[0]}).flatten; <span class="s2">//feedforward coefficients</span></p>
<p class="p8">b=[0,1,1]++b;</p>
<p class="p8">c=<span class="s3">Buffer</span>.sendCollection(s, a, 1);</p>
<p class="p8">d=<span class="s3">Buffer</span>.sendCollection(s, b, 1);</p>
<p class="p8">)</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p8">{<span class="s3">Pan2</span>.ar(<span class="s3">LPF</span>.ar((<span class="s3">NL</span>.ar(<span class="s3">AudioIn</span>.ar,c.bufnum, d.bufnum,10000, 1000)*0.1).clip2(1.0), <span class="s3">MouseX</span>.kr(100,10000,<span class="s4">'exponential'</span>)),<span class="s3">LFNoise2</span>.kr(0.5))}.play</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p9"><br></p>
<p class="p8">(</p>
<p class="p1">//limited update</p>
<p class="p8">r= {</p>
<p class="p8"><span class="s3">inf</span>.do{</p>
<p class="p8">e= a.copy;<span class="Apple-converted-space"> </span></p>
<p class="p8">f=b.copy;<span class="Apple-converted-space"> </span></p>
<p class="p9"><br></p>
<p class="p8">10.do{<span class="s3">|i|</span> e[3*i+1]=0.7.rand2;<span class="Apple-converted-space">  </span>e[3*i+2]=exprand(0.1,6.1);<span class="Apple-converted-space">  </span>};<span class="Apple-converted-space"> </span></p>
<p class="p8">6.do{<span class="s3">|i|</span> f[3*i+1]=0.2.rand2;<span class="Apple-converted-space">  </span>f[3*i+2]=exprand(0.1,6.1);<span class="Apple-converted-space">  </span>};<span class="Apple-converted-space"> </span></p>
<p class="p9"><br></p>
<p class="p8">c.sendCollection(e);</p>
<p class="p8">d.sendCollection(f);</p>
<p class="p9"><br></p>
<p class="p8">1.0.wait;</p>
<p class="p8">}</p>
<p class="p8">}.fork;<span class="Apple-converted-space"> </span></p>
<p class="p8">)</p>
<p class="p9"><br></p>
<p class="p8">r.stop;</p>
<p class="p9"><br></p>
<p class="p9"><br></p>
</body>
</html>
